package com.cs.controller;

import com.cs.domain.Category;
import com.cs.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/prod/category")
@Api(tags = "分类管理的接口")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 查询分类的接口
     * @return
     */
    @GetMapping("/table")
    @ApiOperation(value = "查询分类的接口")
    @PreAuthorize("hasAuthority('prod:category:page')")
    public ResponseEntity<List<Category>> findAllTable(){
        List<Category> categoryList = categoryService.list();
        return ResponseEntity.ok(categoryList);
    }

    /**
     * 查询商品的父级分类
     * @return
     */
    @ApiOperation(value = "查询商品的父级分类的接口")
    @GetMapping("/listCategory")
    @PreAuthorize("hasAuthority('prod:category:page')")
    public ResponseEntity<List<Category>> findParentCategory(){
        List<Category> categories=categoryService.findParentCategory();
        return ResponseEntity.ok(categories);
    }

    /**
     * 添加商品分类的接口
     * @return
     */
    @PostMapping
    @PreAuthorize("hasAuthority('prod:category:save')")
    @ApiOperation(value = "添加商品分类的接口")
    public ResponseEntity<Void> addCategory(@RequestBody Category category){
        categoryService.save(category);
        return ResponseEntity.ok().build();
    }


}
